package com.ftqh.tutorial.api.v1;

import com.ftqh.tutorial.Entity.Order;
import com.ftqh.tutorial.exception.EntityNotFoundException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class OrderServiceV1 {
    public Order findOne(Long id) throws Exception{
        if(String.valueOf(id).length()<11)
            throw new Exception("Invalid Order Id");
        Order order = Order.builder()
                .id(id)
                .userId(100001)
                .amount(BigDecimal.valueOf(12.5))
                .createdAt(System.currentTimeMillis())
                .createdBy(1)
                .build();
        Optional<Order> orderOptional = Optional.ofNullable(order);
        return orderOptional.orElseThrow(() -> new EntityNotFoundException("No order with requested id: " + id));
    }
    public List<Order> findByUserId(Integer userId) throws Exception{
        List<Order> orders = new ArrayList();
        for(int i=0;i<3;i++){
            Order order = Order.builder().
                    id(20170901432678L)
                    .userId(userId)
                    .amount(BigDecimal.valueOf(Long.valueOf(String.valueOf(i))))
                    .createdAt(System.currentTimeMillis()).build();
            orders.add(order);
        }
        return orders;
    }
}
